var slidertimers = [];

function slider(sid, pid, nid, delai){
	nextSlide(sid, pid, nid, delai);
	slidertimers[sid] = window.setTimeout("slider('" + sid + "','" + pid + "','" + nid + "'," + delai + ");", delai);
}

function nextSlide(sid, pid, nid, delai){
	var $Slides = $(sid);
	$(nid).unbind("click");
	$Slides.animate(
		{marginLeft:"-=315px"},
		1000,
		function(){
				$Slides.data("currentSlide",$Slides.data("currentSlide")+1);
				if($Slides.data("currentSlide") > $Slides.data("nbSlides")){
					$Slides
						.data("currentSlide",1)
						.css({marginLeft:"-315px"});
				}
				window.clearTimeout(slidertimers[sid]);
				slidertimers[sid] = window.setTimeout("slider('" + sid + "','" + pid + "','" + nid + "'," + delai + ");", delai);
				$(nid).bind("click",function(){nextSlide(sid, pid, nid, delai);});
			}
	);
}

function prevSlide(sid, pid, nid, delai){
	var $Slides = $(sid);
	$(pid).unbind("click");
	$Slides.animate(
		{marginLeft:"+=315px"},
		1000,
		function(){
				$Slides.data("currentSlide",$Slides.data("currentSlide")-1);
				if($Slides.data("currentSlide") == 0){
					$Slides
						.data("currentSlide",$Slides.data("nbSlides"))
						.css({marginLeft:-(315*$Slides.data("currentSlide"))});
				}
				window.clearTimeout(slidertimers[sid]);
				slidertimers[sid] = window.setTimeout("slider('" + sid + "','" + pid + "','" + nid + "'," + delai + ");", delai);
				$(pid).bind("click",function(){prevSlide(sid, pid, nid, delai);});
			}
	);
}
/*Fonctions de recherche*/
function RechercheGoogle(){
	var urlGoogle = "http://www.google.fr/search?q=";
	try {
		if($("#fldGoogleSearsh").val() !== 'Recherche Google' && $("#fldGoogleSearsh").val() !== '') {
			var url = urlGoogle + encodeURIComponent($("#fldGoogleSearsh").val());
			_paq.push(['trackPageView', 'Recherche/Google']);
			window.open(url, "GoogleSearch");
			$("#fldGoogleSearsh").val('');
			$("#fldGoogleSearsh").blur();
		}
	}
	catch (ex) {
	}
}

function ChangePersonne(ctl){	
    var linkURL = "/Pages/PeopleSearch.aspx?k=" + ctl.value.toLowerCase();
	$("#onVplinkSearsh").attr('checked','checked');
	window.location.href = linkURL;
}

function RechercheAnnuaire(){
	var urlContact = "/Pages/PeopleSearch.aspx?k=";
	try {
		if($("#fldContactSearchAnnuaire").val() !== 'Recherche Google' && $("#fldContactSearchAnnuaire").val() !== ''){
			var url = urlContact + encodeURIComponent($("#fldContactSearchAnnuaire").val());
			_paq.push(['trackPageView', 'Recherche/Annuaire']);
			$("#fldContactSearchAnnuaire").val('Recherche Annuaire');
			$("#fldContactSearchAnnuaire").blur();
			$("#onVplinkSearsh").attr('checked','checked');
			window.location.assign(url);
		}
	}
	catch (ex) {
	}
}

function RechercheVPLink(){
	var $inputVPLink = $("#fldVplinkSearsh");
	if($("#fldVplinkSearsh").val() !== 'Recherche VPLink' && $("#fldVplinkSearsh").val() !== '') {
	    var urlSearchVPLink = "/Pages/Search.aspx?k="; //&cs=Ce%20site&u=http%3A%2F%2Fvplink
		try {
			var url = urlSearchVPLink + encodeURIComponent($inputVPLink.val());
			_paq.push(['trackPageView', 'Recherche/VPLink']);
			$("#fldVplinkSearsh").val('');
			$("#fldVplinkSearsh").blur();
			window.location.assign(url);
		}
		catch (ex) {
		}
	}
}

function InputRechercheOnBlur(input,value){
	if (input.val() == '') {
		input.val(value);
		input.css('color','#ccc');
	}
}

function InputRechercheOnFocus(input,value){
	if(input.val() === value){
		input.val('');
		input.css('color','#000');
	}
}


$(function(){
	var rows = [{id:"slides", next:"nextSlide", prev:"prevSlide", delai:5000},
				{id:"slides2", next:"nextSlide2", prev:"prevSlide2", delai:7500},
				{id:"slides3", next:"nextSlide3", prev:"prevSlide3", delai:8500}];
				
	$.each(rows, function(k,v){
		var sid = "#" + v.id;
		var pid = "#" + v.prev;
		var nid = "#" + v.next;
		var delai = v.delai;
		
		var $Slides = $(sid);
		var _step = $Slides.find("li:first").width();
		$Slides
			.data("currentSlide",1)
			.data("nbSlides",$Slides.find("li").size());
		$Slides
			.find("li:last")
				.clone()
				.prependTo(sid);

		$Slides
			.find("li:first")
				.next()
				.clone()
				.appendTo(sid);

		$Slides		
			.find("li:first")
				.addClass("clone")
			.end()
			.find("li:last")
				.addClass("clone")
			.end()
			.css({marginLeft:-_step});

		$Slides.width($Slides.find("li").size()*_step);
		
		$(nid).bind("click",function(){nextSlide(sid, pid, nid, delai);});
		$(pid).bind("click",function(){prevSlide(sid, pid, nid, delai);});
		
		slidertimers[sid] = window.setTimeout("slider('" + sid + "','" + pid + "','" + nid + "'," + delai + ");", delai);
	});
		$("input[name=VplinkSearsh]").on('change', function(){
		var checkedSearch = $("input[name=VplinkSearsh]:checked").val();
		switch(checkedSearch)
		{
			case "googleVplinkSearsh":
			{
				$('#fldVplinkSearsh').val('Recherche VPLink');
				$('#fldVplinkSearsh').hide();
				$('#btnVplinkSearsh').hide();
				$('#fldContactSearchAnnuaire').val('Recherche Annuaire');
				$('#fldContactSearchAnnuaire').hide();
				$('#btnAnnuaireSearsh').hide();
				$('#btnGoogleSearsh').show();
				$('#fldGoogleSearsh').show();
				break;
			}
			case "onVplinkSearsh":
			{
				$('#fldContactSearchAnnuaire').val('Recherche Annuaire');
				$('#fldContactSearchAnnuaire').hide();
				$('#btnAnnuaireSearsh').hide();
				$('#fldGoogleSearsh').val('Recherche Google');
				$('#fldGoogleSearsh').hide();
				$('#btnGoogleSearsh').hide();
				$('#btnVplinkSearsh').show();
				$('#fldVplinkSearsh').show();
				break;
			}
			case "directoryVplinkSearsh":
			{
				$('#fldVplinkSearsh').val('Recherche VPLink');
				$('#fldVplinkSearsh').hide();
				$('#btnVplinkSearsh').hide();
				$('#fldGoogleSearsh').val('Recherche Google');
				$('#fldGoogleSearsh').hide();
				$('#btnGoogleSearsh').hide();
				$('#fldContactSearchAnnuaire').show();
				$('#btnAnnuaireSearsh').show();
				break;
			}

		}
	});
	
/*Recherche VPLink*/
	$("#fldVplinkSearsh").on("blur",function(){InputRechercheOnBlur($("#fldVplinkSearsh"),'Recherche VPLink');});
	$("#fldVplinkSearsh").on("focus", function(){InputRechercheOnFocus($("#fldVplinkSearsh"),'Recherche VPLink');});
	$('#btnVplinkSearsh').on('click',RechercheVPLink);
	$('#fldVplinkSearsh').on('keydown',function(e){
		try {
			var evt = e || window.event;
			var code = evt.which || evt.keyCode;
			if(code === 13){
				RechercheVPLink();
			}
		}
		catch (ex) {
		}
	});
	
	/*Recherche Google*/
	$("#fldGoogleSearsh").on("blur",function(){InputRechercheOnBlur($("#fldGoogleSearsh"),'Recherche Google');});
	$("#fldGoogleSearsh").on("focus", function(){InputRechercheOnFocus($("#fldGoogleSearsh"),'Recherche Google');});
	$('#btnGoogleSearsh').on('click',RechercheGoogle);
	$('#fldGoogleSearsh').on('keydown',function(e){
		try {
			var evt = e || window.event;
			var code = evt.which || evt.keyCode;
			if(code === 13){
				RechercheGoogle();
			}
		}
		catch (ex) {
		}
	});
	/*Recherche Annuaire*/
	$("#fldContactSearchAnnuaire").on("blur",function(){InputRechercheOnBlur($("#fldContactSearchAnnuaire"),'Recherche Annuaire');});
	$("#fldContactSearchAnnuaire").on("focus", function(){InputRechercheOnFocus($("#fldContactSearchAnnuaire"),'Recherche Annuaire');});
	$('#btnAnnuaireSearsh').on('click',RechercheAnnuaire);
});
